package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.dao.mapper.TkItemMediaMapper;
import com.ddwl.goods.dao.model.ItemMedia;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * 类ItemMediaDao.java的实现描述：TODO
 *
 * @author zach
 * @Date :2020-10-29 10:52.
 */
@Repository
public class ItemMediaDao extends TkDao<TkItemMediaMapper, ItemMedia> {

    @Resource
    private TkItemMediaMapper tkItemMediaMapper;

    public void deleteByItem(String itemId) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId);
        ItemMedia im = new ItemMedia();
        im.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        im.setGmtModified(Instant.now());
        tkItemMediaMapper.updateByExampleSelective(im, example);
    }

    public ItemMedia findByItemIdAndIndex(String itemId, Integer mediaIndex) {
        ItemMedia entity = new ItemMedia();
        entity.setItemId(itemId);
        entity.setMediaIndex(mediaIndex);
        return tkItemMediaMapper.selectOne(entity);
    }

    public void updateByItemIdAndIndex(String itemId, Integer mediaIndex, ItemMedia media) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId)
                .andEqualTo("mediaIndex", mediaIndex);
        tkItemMediaMapper.updateByExampleSelective(media, example);
    }

    public List<ItemMedia> findByItemIds(List<String> itemIds) {
        Example example = getExample();
        example.and().andIn("itemId", itemIds);
        return tkItemMediaMapper.selectByExample(example);
    }

}
